package wicresoft.controller.gongXu;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.BusinessMajorServices;
import services.dictionary.MajorTypeServices;
import services.staff.StaffInfoServices;
import util.JsonUtil;
import wicresoft.controller.common.CommonResult;
import wicresoft.controller.common.Properties;
import wicresoft.controller.common.UserIdHandle;
import wicresoft.orm.entity.GxCheckPlan;
import wicresoft.orm.entity.GxExpertGroup;
import wicresoft.services.gongXu.GxCheckPlanServices;
import wicresoft.services.gongXu.GxExpertGroupServices;
import wt.entity.dictionary.MajorType;
import wt.webPlugin.ZTree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/12.
 */
@Controller
@RequestMapping("httpPage/gongXu")
public class GongXuAddBusinessController {
    @Autowired
    GxCheckPlanServices gxCheckPlanServices;
    @Autowired
    GxExpertGroupServices gxExpertGroupServices;
    @Autowired
    UserIdHandle userIdHandle;
    @Autowired
    BusinessInfoServices businessInfoServices;

    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    BusinessMajorServices businessMajorServices;
    @Autowired
    StaffInfoServices staffInfoServices;


    @RequestMapping(value = "addBusinessPage")
    public String addBusinessPage(@RequestParam(value = "id") String checkPlanId, Map modalMap) throws Exception {
        GxCheckPlan gxCheckPlan = gxCheckPlanServices.selectById(checkPlanId);
        if (gxCheckPlan == null) {
            modalMap.put("errorMessage", "id 数据无效");
            return "common/errorPage";
        }


        if (businessInfoServices.selectByBusinessCode(gxCheckPlan.getAcceptedId()) != null) {//业务已经存在，直接跳转到抽取。
            return "redirect:extractExpertPage?checkPlanId=" + checkPlanId + "&businessCode=" + gxCheckPlan.getAcceptedId();
        } else {
            String managerUserId = userIdHandle.getNewUserID(gxCheckPlan.getOpId());
            if (gxCheckPlan == null) {
                modalMap.put("errorMessage", "managerUserId 数据无效");
                return "common/errorPage";
            }

            modalMap.put("businessCode", gxCheckPlan.getAcceptedId());
            modalMap.put("companyName", gxCheckPlan.getUnitName());
            modalMap.put("companyCode", gxCheckPlan.getUnitCode());
            modalMap.put("address", gxCheckPlan.getProduceAddr());
            modalMap.put("businessType", gxCheckPlan.getDetailOpType());
            modalMap.put("applicationTime", gxCheckPlan.getApplyTime());
            modalMap.put("acceptTime", gxCheckPlan.getAcceptTime());
            modalMap.put("itemNum", "43");
            modalMap.put("managerUserId", managerUserId);
            modalMap.put("observerUserId", "3940");
            modalMap.put("productName", gxCheckPlan.getProductName());
            modalMap.put("oldMajor", gxCheckPlan.getProductName());//专业和产品相同
            return "gongXu/businessInfo/addBusiness";
        }

    }

    @ResponseBody
    @RequestMapping(value = "addBusiness", produces = "application/json;charset=UTF-8")
    public Object addBusiness(String businessCode, String companyName, String companyCode, String address,
                              String productName, String businessType, String applicationTime, String acceptTime,
                              String itemNum, String managerUserId, String observerUserId, String majorNumList) throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("companyName", companyName);
        data.put("companyCode", companyCode);
        data.put("address", address);
        data.put("productName", productName);
        data.put("businessType", businessType);
        data.put("applicationTime", applicationTime);
        data.put("acceptTime", acceptTime);
        data.put("itemNum", itemNum);
        data.put("managerUserId", managerUserId);
        data.put("observerUserId", observerUserId);

        data.put("majorNumList", majorNumList);
        return Properties.sendData(Properties.addBusinessInfo, data, Properties.uid, Properties.key);

    }


    @ResponseBody
    @RequestMapping(value = "addBusinessExpert", produces = "application/json;charset=UTF-8")
    public Object addExpert(String id, String startTime, String endTime) throws Exception {

        GxCheckPlan gxCheckPlan = gxCheckPlanServices.selectById(id);
        if (gxCheckPlan == null) {
            CommonResult commonResult = new CommonResult();
            commonResult.setCode(CommonResult.ZJ005);
            commonResult.setMessage("id 未找到数据");
            return commonResult;
        } else {
            List<GxExpertGroup> gxExpertGroupList = gxExpertGroupServices.selectByItemId(id);
            if (gxExpertGroupList.isEmpty()) {
                CommonResult commonResult = new CommonResult();
                commonResult.setCode(CommonResult.ZJ005);
                commonResult.setMessage("专家还未抽取");
                return commonResult;
            } else {
                String leaderIdCard = null, crewIdCardList = "";

                for (GxExpertGroup gxExpertGroup : gxExpertGroupList) {
                    if (leaderIdCard == null && "ybdd_common_dic_export_dute_01".equals(gxExpertGroup.getDuty())) {//组长
                        leaderIdCard = gxExpertGroup.getIdentityNumber();
                    } else {
                        crewIdCardList = crewIdCardList + "," + gxExpertGroup.getIdentityNumber();
                    }
                }
                if (crewIdCardList.indexOf(",") == 0) {
                    crewIdCardList = crewIdCardList.substring(1);
                }

                Map<String, Object> data = new HashMap();
                data.put("businessCode", gxCheckPlan.getAcceptedId());
                data.put("leaderIdCard", leaderIdCard);
                data.put("crewIdCardList", crewIdCardList);
                data.put("startTime", startTime);
                data.put("endTime", endTime);
                return Properties.sendData(Properties.addBusinessExperts, data, Properties.uid, Properties.key);
            }
        }
    }


    @ResponseBody
    @RequestMapping(value = "addBusinessResult", produces = "application/json;charset=UTF-8")
    public Object addBusinessResult(String id, String startTime, String endTime, String result) throws Exception {

        GxCheckPlan gxCheckPlan = gxCheckPlanServices.selectById(id);
        if (gxCheckPlan == null) {
            CommonResult commonResult = new CommonResult();
            commonResult.setCode(CommonResult.ZJ005);
            commonResult.setMessage("id 未找到数据");
            return commonResult;
        } else {
            Map<String, Object> data = new HashMap();
            data.put("businessCode", gxCheckPlan.getAcceptedId());
            data.put("result", result);
            data.put("startTime", startTime);
            data.put("endTime", endTime);
            return JsonUtil.json2Obj(Properties.sendData(Properties.addBusinessResult, data, Properties.uid, Properties.key), Map.class);
        }

    }


    /**
     * 获取专业树
     *
     * @param
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "getProfessionTree", produces = "application/json;charset=UTF-8")
    public Object getProfessionSetTree() throws Exception {
        List<MajorType> majorTypeList = majorTypeServices.selectMajorType(Properties.GX_ITEM_ID, true);
        JSONArray jsonArray = new JSONArray();
        for (MajorType majorType : majorTypeList) {
            JSONObject jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName(), majorType.getNum());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }


}
